define(['egis'], function (egis) {
    Window.deserializer = Window.deserializer || new egis.core.Deserializer();
    Window.container = Window.container || {};
    dsBridge.register('create_RestWRMSService', function (sourceId, url, authType, clientId, clientSecret, tokenUrl) {
        //var restHttp = Window.container[http];
        var restGeoService = new egis.ews.RestWRMSService({
            url: url,
            //http: restHttp,
            deserializer: Window.deserializer,
            authType: authType,
            clientId: clientId,
            clientSecret: clientSecret,
            tokenUrl: tokenUrl
        });
        Window.container[sourceId] = restGeoService;
    })

    dsBridge.registerAsyn('RestWRMSService_getLayer', function (sourceId, wrcsName, nodeId, withRelation, responseCallback) {
        var restWRMSService = Window.container[sourceId];
        restWRMSService.getLayer(wrcsName, nodeId).then(function (value) {
            responseCallback(value);
        })
    })

    dsBridge.registerAsyn('RestWRMSService_findById', function (sourceId, layer, id, withRelation, responseCallback) {
        var restWRMSService = Window.container[sourceId];
        restWRMSService.findById(layer, id, withRelation).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('RestWRMSService_find', function (sourceId, layer, filter, withRelation, responseCallback) {
        try {
            var restWRMSService = Window.container[sourceId];
            filter = Window.deserializer.createJsInstance(JSON.parse(filter));
            restWRMSService.find(layer, filter, withRelation).then(function (value) {
                responseCallback(JSON.stringify(value));
            })
        } catch (e) {
            alert(e)
        }

    })

    dsBridge.registerAsyn('RestWRMSService_deleteById', function (sourceId, layer, id, responseCallback) {
        var restWRMSService = Window.container[sourceId];
        restWRMSService.deleteById(layer, id).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('RestWRMSService_delete', function (sourceId, layer, filter, responseCallback) {
        var restWRMSService = Window.container[sourceId];
        filter = Window.deserializer.createJsInstance(JSON.parse(filter));
        restWRMSService.delete(layer, filter).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })

    dsBridge.registerAsyn('RestWRMSService_create', function (sourceId, layer, withRelation, responseCallback) {
        var restWRMSService = Window.container[sourceId];
        restWRMSService.create(layer, withRelation).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    dsBridge.registerAsyn('RestWRMSService_save', function (sourceId, layer, entity, responseCallback) {

        var restWRMSService = Window.container[sourceId];
        entity = Window.deserializer.createJsInstance(JSON.parse(entity));
        restWRMSService.save(layer, entity).then(function (value) {
            //responseCallback(JSON.stringify(value));
            responseCallback(value);
        })

    })

    dsBridge.registerAsyn('RestWRMSService_update', function (sourceId, layer, entity, responseCallback) {
        var restWRMSService = Window.container[sourceId];
        entity = Window.deserializer.createJsInstance(JSON.parse(entity));
        restWRMSService.update(layer, entity).then(function (value) {
            //responseCallback(JSON.stringify(value));
            responseCallback(value);
        })
    })
    dsBridge.registerAsyn('RestWRMSService_batchSave', function (sourceId, layer, entities, responseCallback) {
        try {
            var restWRMSService = Window.container[sourceId];
            entities = Window.deserializer.createJsInstance(JSON.parse(entities));
            entities.forEach(function (entity) {
                entity['$type'] = 'ResourceEntity,http://www.Gs.com';
            })
            restWRMSService.batchSave(layer, entities).then(function (value) {
                // responseCallback(JSON.stringify(value));
                responseCallback(value);
            })
        }
        catch (e) {
            alert(e);
        }


    })
    dsBridge.registerAsyn('RestWRMSService_pageQuery', function (sourceId, layer, page, size, filter, responseCallback) {
        var restWRMSService = Window.container[sourceId];
        filter = Window.deserializer.createJsInstance(JSON.parse(filter));
        restWRMSService.pageQuery(layer, page, size, filter).then(function (value) {
            responseCallback(JSON.stringify(value));
        })
    })
    /* dsBridge.registerAsyn('RestWRMSService_compreQuery', function (sourceId, layer, filter, responseCallback) {
         var restWRMSService = Window.container[sourceId];
         filter = Window.deserializer.createJsInstance(JSON.parse(filter));
         restWRMSService.compreQuery(layer, filter).then(function (value) {
             responseCallback(JSON.stringify(value));
         })
     })*/
})